package ar.com.freesales.service;

import java.util.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ar.com.freesales.dao.GenericDao;
import ar.com.freesales.entity.ChangeLog;
import ar.com.freesales.entity.DateBatch;
import ar.com.freesales.entity.DateBatchSequence;
import ar.com.freesales.entity.Ota;
import ar.com.freesales.entity.UserDetail;
import ar.com.freesales.enums.ActionEnum;
import ar.com.freesales.enums.RoomTypeEnum;
import ar.com.freesales.enums.StatusEnum;

@Service
public class ProcessBatchService {
	
	@Autowired
	private GenericDao genericDao;
	
	@Autowired
	private DateBatchService dateBatchService;
	
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public void processBatch(Set<Date> dates, RoomTypeEnum roomTypeEnum, Set<Ota> otas, UserDetail userDetail, Double value){
		//genero el lote de fechas
		Long idLote = dateBatchService.buildDateBatch(dates);
		
		
		//armo el changeLog por cada ota
		ChangeLog changeLog = null; 
		for (Ota ota : otas) {
			changeLog = new ChangeLog();
			changeLog.setActionEnum(ActionEnum.CHANGE_RATE);
			changeLog.setDate(new Date());
			changeLog.setIdDateBatch(idLote);
			changeLog.setOta(ota);
			changeLog.setRoomTypeEnum(roomTypeEnum);
			changeLog.setStatusEnum(StatusEnum.INIT);
			changeLog.setUserDetail(userDetail);
			changeLog.setValue(value);
		}
		
		genericDao.save(changeLog);
		
	}

}
